package com.yuanduo_app.widget;

import android.content.Intent;
import android.text.TextUtils;
import com.baozi.Zxing.utils.ZXingUtils;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.xujl.fastlib.utils.LogS;
import com.xujl.task.Emitter;
import com.xujl.task.RxExecutor;
import com.xujl.task.Task;
import com.zhongjh.albumcamerarecorder.settings.MultiMediaSetting;

import com.yuanduo_app.bridge.DataModule;
import com.yuanduo_app.event.MainActivityLifeEvent;
import com.yuanduo_app.event.PhotoQrCodeEvent;
import com.yuanduo_app.manager.RNModuleManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class QRCodeViewManager extends SimpleViewManager<QrCodeView> {
    private static final String TAG = "QRCodeViewManager";
    private static final int START_ANNIMATION = 1;
    private static final int STOP_ANNIMATION = 2;
    private static final int START_SCAN = 3;
    private static final int STOP_SCAN = 4;
    private static final int SCARN_PHOTO_LIBRARY_IMAGE = 5;
    private static final int ON_DESTROY = 6;
    private QrCodeView mQrCodeView;

    public static void onPhotoQrCodeResult (Intent data) {
        if (data == null) {
            return;
        }
        List<String> result = MultiMediaSetting.obtainPathResult(data);
        try {
            if (result != null && result.size() != 0) {
                //解析图片
                analysisImage(result.get(0));
            }
        }catch (Exception e){

        }
    }

    @NonNull
    @Override
    public String getName () {
        return "QRCodeView";
    }

    @NonNull
    @Override
    protected QrCodeView createViewInstance (@NonNull ThemedReactContext reactContext) {
        mQrCodeView = new QrCodeView(reactContext);

        return mQrCodeView;
    }

    @ReactProp(name = "torchMode")
    public void torchMode (QrCodeView qrCodeView, boolean torchMode) {
        if (torchMode) {
            qrCodeView.lightOn();
        } else {
            qrCodeView.lightOff();
        }
    }

    @Nullable
    @Override
    public Map<String, Integer> getCommandsMap () {
        Map<String, Integer> map = new HashMap<>();
        map.put("startAnnimation", START_ANNIMATION);
        map.put("stopAnnimation", STOP_ANNIMATION);
        map.put("startScan", START_SCAN);
        map.put("stopScan", STOP_SCAN);
        map.put("scarnPhotoLibraryImage", SCARN_PHOTO_LIBRARY_IMAGE);
        map.put("onDestroy", ON_DESTROY);
        return map;
    }


    @Override
    public void receiveCommand (@NonNull QrCodeView qrCodeView, int commandId, @Nullable ReadableArray args) {
        super.receiveCommand(qrCodeView, commandId, args);
        switch (commandId) {
            case START_ANNIMATION:
                LogS.i(TAG, "START_ANNIMATION");
                qrCodeView.startAnimation();
                break;
            case STOP_ANNIMATION:
                LogS.i(TAG, "STOP_ANNIMATION");
                qrCodeView.stopAnimation();
                break;
            case START_SCAN:
                LogS.i(TAG, "START_SCAN");
                qrCodeView.startScan();
                try {
                    EventBus.getDefault().register(this);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case STOP_SCAN:
                LogS.i(TAG, "STOP_SCAN");
                qrCodeView.stopScan();
                break;
            case SCARN_PHOTO_LIBRARY_IMAGE:
                LogS.i(TAG, "SCARN_PHOTO_LIBRARY_IMAGE");
                EventBus.getDefault().post(new PhotoQrCodeEvent());
                break;
            case ON_DESTROY:
                LogS.i(TAG, "ON_DESTROY");
                qrCodeView.onDestroy();
                break;
            default:

                break;

        }
    }


    @Subscribe
    public void onMainActivityLifeEvent (MainActivityLifeEvent event) {
        if (event.getState() == MainActivityLifeEvent.ON_RESUME) {
            mQrCodeView.onResume();
        } else if (event.getState() == MainActivityLifeEvent.ON_PAUSE) {
            mQrCodeView.onPause();
        }
    }

    private static void analysisImage (final String photoPath) {
        if (TextUtils.isEmpty(photoPath)) {
            sendData("");
            return;
        }
        RxExecutor.getInstance()
                .executeTask(new Task<String>() {
                    @Override
                    public void run (Emitter<String> emitter) throws Exception {
                        super.run(emitter);
                        emitter.next(ZXingUtils.syncDecodeQRCode(photoPath));
                    }

                    @Override
                    public void onNext (String result) {
                        super.onNext(result);
                        // 数据返回
                        sendData(result);
                    }
                });

    }

    private static void sendData (String data) {
        final DataModule dataModule = RNModuleManager.getInstance().getModule(DataModule.class);
        dataModule.sendEvent(DataModule.ON_SCAN_FINISH_BLOCK, data);
    }
}
